package com.crane.framework.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.framework.constant.SystemConst;
import com.crane.framework.dto.UpdateSelfDTO;
import com.crane.framework.dto.UserPageDTO;
import com.crane.framework.entity.Role;
import com.crane.framework.service.IFileService;
import com.crane.framework.service.IUserRoleService;
import com.crane.framework.service.IUserService;
import com.crane.framework.dto.UserAddDTO;
import com.crane.framework.utils.CommonUtil;
import com.crane.framework.utils.SmUtil;
import com.crane.framework.vo.UserDetailVO;
import com.crane.framework.dto.UserUpdateDTO;
import com.crane.framework.entity.User;

import com.crane.framework.dto.common.IdParamDTO;
import com.crane.framework.utils.SystemUtil;

import com.crane.framework.vo.UserListVO;
import com.crane.framework.vo.common.SelectVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "用户操作 API")
@RestController
@RequestMapping("/api/v1.0/user")
public class UserController {

  @Resource private IUserService userService;
  @Resource private IUserRoleService userRoleService;
  @Resource private IFileService fileService;

  @SaCheckRole("root")
  @Operation(summary = "新增")
  @PostMapping("/add")
  public void add(@RequestBody @Valid UserAddDTO addDTO) {
    userService.addUser(addDTO);
  }

  @SaCheckRole("root")
  @Operation(summary = "删除")
  @PostMapping("/remove")
  public void remove(@RequestBody @Valid IdParamDTO idParamDTO) {
    userService.removeUser(idParamDTO.getId());
  }

  @SaCheckRole("root")
  @Operation(summary = "更新")
  @PostMapping("/update")
  public void update(@RequestBody @Valid UserUpdateDTO updateDTO) {
    userService.updateUser(updateDTO);
  }

  @SaCheckRole("root")
  @Operation(summary = "详情")
  @PostMapping("/detail")
  public UserDetailVO detail(@RequestBody @Valid IdParamDTO idParamDTO) {
    User user = userService.getById(idParamDTO.getId());
    List<Role> roleList = userRoleService.getRoleListByUserId(user.getId());
    UserDetailVO userDetailVO = SystemUtil.buildResource(user, UserDetailVO.class);
    if (CommonUtil.notNullOrEmpty(roleList)) {
      List<String> roleIdList = roleList.stream().map(Role::getId).toList();
      userDetailVO.setRoleIdList(roleIdList);
    }
    return userDetailVO;
  }

  @SaCheckRole("root")
  @Operation(summary = "获取用户列表")
  @PostMapping("/pageUserList")
  public Page<UserListVO> pageUserList(@RequestBody UserPageDTO userPageDTO) {
    return userService.getUserList(userPageDTO);
  }

  @SaCheckRole("root")
  @Operation(summary = "获取用户下拉列表")
  @PostMapping("/getUserList")
  public List<SelectVO> getUserList() {
    List<User> userList = userService.list();
    if (CommonUtil.isNullOrEmpty(userList)) {
      return new ArrayList<>();
    }

    return userList.stream().map(i -> new SelectVO(i.getNickname(), i.getId())).toList();
  }

  @Operation(summary = "获取自己的信息")
  @PostMapping("/selfInfo")
  public UserDetailVO selfInfo() {
    Object loginId = StpUtil.getLoginId();
    User user = userService.getById(loginId.toString());
    UserDetailVO userDetailVO = SystemUtil.buildResource(user, UserDetailVO.class);
    fileService.transfer(userDetailVO);
    return userDetailVO;
  }

  @Operation(summary = "更新自己的信息")
  @PostMapping("/updateSelfInfo")
  public void updateSelfInfo(@RequestBody @Valid UpdateSelfDTO updateSelfDTO) {
    User user = SystemUtil.buildResource(updateSelfDTO, User.class);
    Object loginId = StpUtil.getLoginId();
    user.setId(loginId.toString());
    String password = updateSelfDTO.getPassword();
    if (CommonUtil.notNullOrEmpty(password)) {
      user.setPassword(SmUtil.sm3(password));
    }
    userService.updateById(user);
  }
}
